//
//  SpeakerArc.swift
//  codelabs
//
//  Created by 雷雷 on 27/05/2025.
//

import SwiftUI


struct SpeakerArc:Shape{
    let speakerIndex:Int
    let totalSeakers:Int
    
    init(speakerIndex: Int, totalSeakers: Int) {
        self.speakerIndex = speakerIndex
        self.totalSeakers = totalSeakers
    }
    
    
    private var degreePerSpeaker:Double{
        360.0/Double(totalSeakers)
    }
    
    private var startAngle:Angle{
        Angle(degrees: degreePerSpeaker * Double(speakerIndex) + 1.0)
    }
    
    private var endAngle:Angle{
        Angle(degrees: startAngle.degrees  + degreePerSpeaker - 1.0)
    }
    
    func path(in rect:CGRect) -> Path {
        let diameter = min(rect.size.width,rect.size.height)
        // 半径
        let r=diameter/2.0
        // 中心点
        let center = CGPoint(x:rect.midX,y:rect.midY)
        
        // 绘制环
        return Path{ path in
            path
                .addArc(
                    center: center,
                    radius: r,
                    startAngle: startAngle,
                    endAngle: endAngle,
                    clockwise: false
                )
        }
    }
}
